package edu.tongji.jiaowu.webapp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.tongji.quickCRUDQ.util.RequestUtil;
import com.tongji.quickCRUDQ.util.StringUtil;

import edu.tongji.jiaowu.service.BasicDataManager;
import edu.tongji.jiaowu.service.GraduateManager;
import edu.tongji.jiaowu.webapp.session.SessionParameter;

public class GraduateCheckController extends MultiActionController {
	private GraduateManager graduateManager; 
	private BasicDataManager basicDataManager;
	
	public ModelAndView batch(HttpServletRequest request, HttpServletResponse response){
		String year = graduateYear(request);
		Map model=new HashMap();
		model.put("graduateYear", year);
		String cnt=graduateManager.count(year);
		model.put("checkedStudents",cnt);
		return new ModelAndView("graduate/batch","model",model);
	}

	private String graduateYear(HttpServletRequest request) {
		SessionParameter sessionParameter = (SessionParameter) request.getSession().getAttribute("SessionParameter");
		String year=sessionParameter.getCurrentSchoolYear();
		String semester=sessionParameter.getCurrentSchoolSemester();
		if ("2".equals(semester)) {//学年第二学期,则毕业年份=学年度+1
			year=(Integer.parseInt(year)+1)+"";
		}
		return year;
	}
	
	public ModelAndView single(HttpServletRequest request, HttpServletResponse response){
		String year = graduateYear(request);
		Map model=basicDataManager.graduateMajorMap(year); 
		model.put("graduateYear", year);
		
		List years=graduateManager.findGraduateYears();
		int num1=years.size();
		StringBuffer sb=new StringBuffer();
		sb.append("[");
		for (int i = 0; i < num1; i++) {
			String gyear=(String) years.get(i);
			if (year.equals(gyear)) {
				continue;
			}
			sb.append("['").append(gyear).append("','").append(gyear).append("'],");
		}
		StringUtil.deleteComma(sb);
		sb.append("]");
		model.put("graduateYearJson", sb.toString());
		return new ModelAndView("graduate/single","model",model);
	}
	
	public ModelAndView singleCheck(HttpServletRequest request, HttpServletResponse response){
		return null;
	}
	
	
	
	public ModelAndView graduateCheck(HttpServletRequest request, HttpServletResponse response){
		String graduateYear=request.getParameter("graduateYear");//毕业年份
		if (graduateYear==null||"".equals(graduateYear)) {
			return null;//TODO mytodo 处理--传入的“毕业年份”为空
		}
		try {
			graduateManager.check(graduateYear);
			RequestUtil.writeJsonSuccess(response, "毕业审核成功");
		} catch (Exception e) {
			RequestUtil.writeJsonException(response, e);
		}
		return null;
		
	}

	public GraduateManager getGraduateManager() {
		return graduateManager;
	}

	public void setGraduateManager(GraduateManager graduateManager) {
		this.graduateManager = graduateManager;
	}

	public BasicDataManager getBasicDataManager() {
		return basicDataManager;
	}

	public void setBasicDataManager(BasicDataManager basicDataManager) {
		this.basicDataManager = basicDataManager;
	}
}
